package com.sysman.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sysman.HttpTools.DataAnalyze;
import com.sysman.HttpTools.HttpRequest;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2018年1月12日 下午12:49:23 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/FeedBackMan")
public class FeedBackController {
	/**
	 * 反馈信息界面初始化
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/FeedBackInfo")
	public ModelAndView FeedBackInfo()throws Exception {
		ModelAndView model = new ModelAndView();
		model.setViewName("View/SystemManage/FeedBackInfo");
		return model;
	}
	/**
	 * 异步加载反馈信息
	 * @param limit
	 * @param offset
	 * @param type
	 * @param check
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/feedbackListAjax")
	public @ResponseBody Map<String,Object> feedbackListAjax(Integer limit,Integer offset,Integer type,String check,HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		String param ="";
		if(check!="") {
			boolean Check=Boolean.parseBoolean(check);
			param="adminloginid="+adminloginid+"&limit="+limit+"&offset="+offset+"&type="+type+"&check="+Check;
		}else {
			param="adminloginid="+adminloginid+"&limit="+limit+"&offset="+offset+"&type="+type+"&check="+check;
		}
		System.out.println("输出这个param："+param);
		Map<String,Object> result = new HashMap<>();
		List<Map<String,Object>> list = new ArrayList<>();
		Integer total=0;
		String msg="";
		String code="";
		try {
			JSONObject json = HttpRequest.sendPost("listfeedback", param);
			System.out.println("输出接收到的json数据是："+json);
			
			msg = json.get("msg").toString();
			code =json.get("code").toString();
			if("100".equals(code)) {
				try {
					if(json.get("data").toString()!="null") {
						JSONObject jsonobjectdata =json.getJSONObject("data");
						JSONArray jsonarrayrows = jsonobjectdata.getJSONArray("rows");
						total = Integer.parseInt(jsonobjectdata.get("total").toString());
						list=DataAnalyze.FeedBackJsonToList(jsonarrayrows);
					}
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
					msg = "数据解析出错！";
					code ="-300";
				}
			}
		} catch (Exception e) {
			System.out.println("出错信息是："+e.getMessage());
			msg = "数据传输出错！";
			code ="-200";
		}
		result.put("msg", msg);
		result.put("rows", list);
		result.put("code", code);
		result.put("total", total);
        
		System.out.println("输出这个result："+result);
		return result;
	}
	/**
	 * 读取反馈信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/readfeedbackinfo")
	public @ResponseBody Map<String,Object> readfeedbackinfo(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		Integer feedbackid = Integer.parseInt(request.getParameter("feedbackid"));
		String param = "adminloginid="+adminloginid+"&feedbackid="+feedbackid;
		
		Map<String,Object> result = new HashMap<>();
		String code="";
		String msg="";
		try {
			JSONObject json = HttpRequest.sendPost("readfeedback", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
		} catch (Exception e) {
			System.out.println("出错了，输出错误信息："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		return result;
	}
}
